using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using System.IO;

namespace PocketLibrary.Blog.NucleusPlugin
{
    public class TagEX
    {
        string _username = "";
        string _userpassword = "";
        public string UserName
        {
            set
            {
                _username = value;
            }
        }

        public string Password
        {
            set
            {
                // md5 hash of password
                _userpassword = HelperUtils.MD5String(value);
            }
        }

        List<string> pTags = new List<string>();

        public List<string> Tags
        {
            get { return pTags; }
            set { pTags = value; }
        }

        int pPostID = -1;

        public int PostID
        {
            get { return pPostID; }
            set
            {
                if (value > 0)
                    pPostID = value;
                else
                    throw new ArgumentOutOfRangeException("PostID cannot be negative!");
            }
        }

        string pluginapiendpoint = "";
        /// <summary>
        /// url to api handler
        /// </summary>
        public string PluginAPIEndPoint
        {
            get { return pluginapiendpoint; }
            set { pluginapiendpoint = value; }
        }

        bool requesting = false;

        protected XmlDocument sendXmlDoc = new XmlDocument();
        protected XmlDocument rcvXmlDoc = new XmlDocument();

        XMLRPCCommunication comm;

        public TagEX()
        {
            comm = new XMLRPCCommunication(this.PluginAPIEndPoint);
            comm.UseStringBuilder = true;
        }

        public void addTagsV()
        {
            this.addTags(PostID, Tags);
        }

        public bool addTags(int postid, List<string> tags)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            sb.Append("<nucleus>");
            sb.Append("<action>addTagEX</action>");
            sb.Append("<params>");
            sb.Append("<username>" + _username + "</username>");
            sb.Append("<password>" + _userpassword + "</password>");
            sb.Append("<postid>" + postid + "</postid>");
            foreach (string tag in tags)
            {
                sb.Append("<tag>" + tag + "</tag>");
            }

            sb.Append("</params>");
            sb.Append("</nucleus>");
            comm.UploadStringBuilder = sb;
            comm.SendReceive();
            rcvXmlDoc.LoadXml(comm.Response);
            XmlNode status = rcvXmlDoc.SelectSingleNode("descendant::status");
            if (status == null)
            {
                return false;
            }
            if (status.InnerText == "ok")
                return true;
            else
                return false;
        }

        public List<string> getAllTags()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            sb.Append("<nucleus>");
            sb.Append("<action>getalltagexs</action>");
            sb.Append("<params>");
            sb.Append("<username>" + _username + "</username>");
            sb.Append("<password>" + _userpassword + "</password>");
            sb.Append("</params>");
            sb.Append("</nucleus>");
            comm.UploadStringBuilder = sb;
            comm.SendReceive();
            rcvXmlDoc.LoadXml(comm.Response);
            XmlNodeList tags = rcvXmlDoc.SelectNodes("descendant::taglist/tag");
            foreach (XmlNode tag in tags)
            {
                Tags.Add(tag.InnerText);
            }
            return Tags;
        }
    }
}

